package com.iemes.controller.mould;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.DevelopProgressFormMap;
import com.iemes.entity.MouldCheckItemFormMap;
import com.iemes.entity.MouldCheckStandardFormMap;
import com.iemes.entity.SurfaceCheckStandardFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.mould.SurfaceCheckStandardMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

@RequestMapping("/surfaceCheckStandard/")
@Controller
public class SurfaceCheckStandardController extends BaseController {

	@Inject
	private SurfaceCheckStandardMapper surfaceCheckStandardMapper;
	
	@RequestMapping("surfaceCheckStandard_list")
	public String examineOrder(Model model){
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/mould/surfaceCheckStandard/surfaceCheckStandard_list";
	}
	
	@ResponseBody
	@RequestMapping("surfaceCheckStandard_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception 
	{
		SurfaceCheckStandardFormMap surfaceCheckStandardFormMap=getFormMap(SurfaceCheckStandardFormMap.class);
		List<Map<String, Object>> surfaceCheckStandard = surfaceCheckStandardMapper.getAll(surfaceCheckStandardFormMap);
		LuogicPaging(pageNow, pageSize, surfaceCheckStandard);
		return pageView;
	}
	
	@RequestMapping("surfaceCheckStandard_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		if(Common.isNotEmpty(id)){
			//根据id查询模具制造外观质量验收单编辑检查标准
			SurfaceCheckStandardFormMap standard=surfaceCheckStandardMapper.getById(id);
			model.addAttribute("standard",standard);
		}
		return Common.BACKGROUND_PATH + "/mould/surfaceCheckStandard/surfaceCheckStandard_edit";
	}
	
	@ResponseBody
	@RequestMapping(value="surfaceCheckStandard_editEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="新开模开发申请",methods="检查标准-编辑检查标准")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity(HttpServletRequest request) throws Exception {
		SurfaceCheckStandardFormMap surfaceCheckStandardFormMap = getFormMap(SurfaceCheckStandardFormMap.class);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		// 获取request
		request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
		surfaceCheckStandardFormMap.put("date", sdf.format(date));
		surfaceCheckStandardFormMap.put("fillPeopleId", userFormMap.getInt("id"));
		baseMapper.editEntity(surfaceCheckStandardFormMap);
		return "success";
	}
	
	@RequestMapping("surfaceCheckStandard_add")
	public String addUI(Model model) throws Exception {
		return Common.BACKGROUND_PATH + "/mould/surfaceCheckStandard/surfaceCheckStandard_add";
	}
	
	@ResponseBody
	@RequestMapping("surfaceCheckStandard_addEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="新开模开发申请",methods="检查标准-新增检查标准")//凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity() throws Exception {
		SurfaceCheckStandardFormMap surfaceCheckStandardFormMap = getFormMap(SurfaceCheckStandardFormMap.class);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		surfaceCheckStandardFormMap.put("date", sdf.format(date));
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
		surfaceCheckStandardFormMap.put("fillPeopleId", userFormMap.getInt("id"));
		baseMapper.addEntity(surfaceCheckStandardFormMap);
		return "success";
	}
	
	@ResponseBody
	@RequestMapping("surfaceCheckStandard_deleteEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="新开模开发申请",methods="检查标准-删除检查标准")//凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		SurfaceCheckStandardFormMap surfaceCheckStandardFormMap = getFormMap(SurfaceCheckStandardFormMap.class);
		String[] ids = getParaValues("ids");
		String arr=ids[0];
		String[] str=arr.split(",");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
		for(int i=0;i<str.length;i++){
			surfaceCheckStandardFormMap.put("date", sdf.format(date));
			surfaceCheckStandardFormMap.put("fillPeopleId", userFormMap.getInt("id"));
			surfaceCheckStandardFormMap.set("status", 0);
			surfaceCheckStandardFormMap.put("id", str[i]);
			baseMapper.editEntity(surfaceCheckStandardFormMap);
		}
		return "success";
	}
}










